<template>
  <div class="goods-comment">
    <div class="head">
      <div class="data">
        <p><span>{{evaluateInfo.evaluateCount}}</span><span>人购买</span></p>
        <p><span>{{evaluateInfo.praisePercent}}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <!-- 全部和有图是固定的，其他是生成的 -->
        <div class="dd">
          <a v-for="(item,idx) in evaluateInfo.tags" :key="item.title" href="javascript:;"
            :class="{active: curIdx===idx}" @click="selectTag(idx,item)">{{item.title}}
            ({{item.tagCount}})</a>

        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a href="javascript:;" @click="changeSort(null)"
        :class="{active: queryParams.sortField==null}">默认</a>
      <a href="javascript:;" @click="changeSort('createTime')"
        :class="{active: queryParams.sortField=='createTime'}">最新</a>
      <a href="javascript:;" @click="changeSort('praiseCount')"
        :class="{active: queryParams.sortField=='praiseCount'}">最热</a>
    </div>
    <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="item in list" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="">
          <span>{{format(item.member.nickname)}}</span>
        </div>
        <div class="body">
          <div class="score">
            <!-- 打分：icon-wjx01：是实心的 icon-wjx02: 是空心的 -->
            <i v-for="it in item.score" :key="it" class="iconfont icon-wjx01"></i>

            <i v-for="it in 5-item.score" :key="it" class="iconfont icon-wjx02"></i>
            <span
              class="attr">{{item.orderInfo.specs.reduce((acc,cur)=>`${acc} ${cur.name}: ${cur.nameValue}`, '')}}</span>
          </div>
          <div class="text">{{item.content}}</div>
          <div class="time">
            <!-- 图片显示区域 -->
            <GoodsCommentImage :pictures="item.pictures" />
            <span>{{item.createTime}}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
          </div>
        </div>
      </div>
    </div>
    <XtxPagination :total="total" @current-change="pageChange" />
  </div>
</template>
<script>
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/goods'
import { ref, inject, reactive, watch } from 'vue'
import GoodsCommentImage from './goods-comment-image.vue'
import XtxPagination from '@/components/xtx-pagination.vue'
export default {
  name: 'GoodsComment',
  components: { GoodsCommentImage, XtxPagination },
  setup () {
    const goodsData = inject('goodsData')
    const evaluateInfo = ref([])
    findCommentInfoByGoods(goodsData.id).then(data => {
      evaluateInfo.value = data.result
      data.result.tags.unshift({ type: 'img', title: '有图', tagCount: data.result.hasPictureCount })
      data.result.tags.unshift({ type: 'all', title: '全部评价', tagCount: data.result.evaluateCount })

      console.log('findCommentInfoByGoods', data)
    })
    // 记录当前激活的索引
    const curIdx = ref(0)
    // 筛选条件
    const queryParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: true,
      tag: null,
      sortField: null,
      sortMethod: null
    })
    // 初始化或筛选条件 改变后，获取列表数据
    const list = ref([])
    const total = ref(0)
    // 修改条件，查询自动执行
    watch(queryParams, () => {
      findCommentListByGoods(goodsData.id, queryParams).then(data => {
        list.value = data.result.items
        total.value = data.result.counts
      })
    }, { immediate: true })
    // 改变排序
    const changeSort = sortFiled => {
      queryParams.sortField = sortFiled

      queryParams.page = 1
    }
    // 更改tag
    const selectTag = (idx, item) => {
      curIdx.value = idx
      console.log(item)
      // 更新条件
      if (item.title === '全部') {
        queryParams.tag = null
        queryParams.hasPicture = null
      } else if (item.title === '有图') {
        queryParams.tag = null
        queryParams.hasPicture = true
      } else {
        queryParams.tag = item.title
        queryParams.hasPicture = null
      }

      // 重置页码
      queryParams.page = 1
    }
    // 更新页码
    const pageChange = page => {
      queryParams.page = page
    }

    const format = (str) => {
      // 1234 ==> 1**4
      // 自己完成
      return `**${str}**`
    }
    return { evaluateInfo, curIdx, queryParams, list, total, changeSort, selectTag, pageChange, format }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
.list {
  padding: 0 20px;
  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;
    .user {
      width: 160px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span {
        padding-left: 10px;
        color: #666;
      }
    }
    .body {
      flex: 1;
      .score {
        line-height: 40px;
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }
    .text {
      color: #666;
      line-height: 24px;
    }
    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
</style>
